/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import excepciones.NonexistentEntityException;
import excepciones.PreexistingEntityException;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.transaction.UserTransaction;
import negocio.Funcion;
import negocio.Tarifa;
import negocio.Ticket;
import negocio.TransaccionVentas;

/**
 *
 * @author Diego
 */
public class FuncionJpaController implements Serializable  {
    
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;
    
    public FuncionJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }

    public FuncionJpaController() {
        emf = Persistence.createEntityManagerFactory("entradasJpaPU");
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    public Funcion findFuncion(int id){
        EntityManager em = getEntityManager();
        try {
            return em.find(Funcion.class, id);
        } finally {
            em.close();
        }
    }
    public void create(Funcion funcion) throws PreexistingEntityException{
      EntityManager em = null;
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(funcion);
            em.getTransaction().commit();
        }
        catch(Exception ex){
            if(findFuncion(funcion.getId()) != null){
                throw new PreexistingEntityException("Funcion Existente",ex);
            }
            throw ex;
        }
        finally{
            if(em != null){
                em.close();
            }
        }  
    }
    public void destroy(Integer id) throws NonexistentEntityException{
        EntityManager em = null;
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            Funcion funcion;
            try{
                funcion = em.getReference(Funcion.class, id);                
            }
            catch(EntityNotFoundException ex){
                 throw new NonexistentEntityException("Funcion Inexistente "+ex.getMessage());    
            }
            em.remove(funcion);
            em.getTransaction().commit();
        }
        finally{
            if (em != null) {
                em.close();
            } 
        }
    }
    public void edit(Funcion funcion){
           EntityManager em = null;
        
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            funcion = em.merge(funcion);
            em.getTransaction().commit();

        }
        finally{
            if(em != null){
                em.close();
            }
        }
    }
    public List<Tarifa> findTarifaEntities(Funcion funcion) {
        return findTarifaEntities(true, -1, -1,funcion);
    }
    private List<Tarifa> findTarifaEntities(boolean all, int maxResults, int firstResult,Funcion funcion) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Tarifa as o where o.unaFuncion = ?1");
            q.setParameter(1, funcion);
            
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
        
    }
    public List<TransaccionVentas> findTransaccionEntities(Funcion funcion){
        return findTransaccionEntities(true,-1,-1,funcion);
    }
    private List<TransaccionVentas> findTransaccionEntities(boolean all, int maxResults, int firstResult,Funcion funcion){
        EntityManager em = getEntityManager();
        try{
            Query q = em.createQuery("Select object(o) from TransaccionVentas as o where o.unaFuncion = ?1");
            q.setParameter(1, funcion);
            if(!all){
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        }
        finally{
            em.close();
        }
    }
    public Set<Ticket> findTicketsEntities(Funcion funcion){
        return findTicketsEntities(true,-1,-1,funcion);
    }
    private Set<Ticket> findTicketsEntities(boolean all, int maxResults,int firstResult, Funcion funcion){
        EntityManager em = getEntityManager();
        List<TransaccionVentas> listaAux;
        Set<Ticket> result = new HashSet();
        TransaccionVentas transaccion;
        try{
            Query q = em.createQuery("Select object(o) from TransaccionVentas as o where o.unaFuncion = ?1");
            q.setParameter(1, funcion);
            if(!all){
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            listaAux = q.getResultList();
            Iterator it = listaAux.iterator();
            while(it.hasNext()){
                transaccion = (TransaccionVentas) it.next();
                result.addAll(transaccion.getTickets());
            }
            return result;            
        }
        finally{
            em.close();
        }
    }
    
}
